/**
 * kl yy-ds @2021
 */
package com.cxps.server.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.product.domain.ProductForm;
import com.cxps.server.product.domain.ProductQuery;
import com.cxps.server.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 商品控制层
 *
 * @author YouCai.Liu
 * @since 2021/10/18
 * ...
 */
@Slf4j
@RestController
@RequestMapping("/product")
@Api(tags = "商品API")
@PreAuthorize("hasAuthority('PRO_MODEL')")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 商品列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "商品列表", response = ApiResponse.class)
    public ApiResponse productList(@ApiParam(name = "query", value = "检索条件")
                                   @RequestBody ProductQuery query) {
        Page page = productService.productList(query);
        return ApiResponse.fireSuccess(page);
    }

    /**
     * 商品添加
     *
     * @param form
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "商品添加", response = ApiResponse.class)
    public ApiResponse addProduct(@ApiParam(name = "form", value = "表单数据")
                                  @RequestBody ProductForm form) {
        try {
            Assert.hasLength(form.getProductName(), "商品名不能为空");
            Assert.hasLength(form.getProductCode(), "商品条码不能为空");
            productService.addProduct(form);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("商品添加失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 根据ID查询商品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据ID查询商品信息", response = ApiResponse.class)
    public ApiResponse getProductById(@PathVariable String id) {
        return ApiResponse.fireSuccess(productService.getProductById(id));
    }

    /**
     * 商品修改
     *
     * @param form
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "商品修改", response = ApiResponse.class)
    public ApiResponse updateProduct(@ApiParam(name = "form", value = "表单数据")
                                     @RequestBody ProductForm form) {
        try {
            Assert.hasLength(form.getId(), "商品ID不能为空");
            Assert.hasLength(form.getProductName(), "商品名不能为空");
            Assert.hasLength(form.getProductCode(), "商品条码不能为空");
            productService.updateProduct(form);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("商品添加失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 商品删除
     *
     * @return
     */
    @GetMapping("/del")
    @ApiOperation(value = "商品删除", response = ApiResponse.class)
    public ApiResponse delProduct(@RequestParam String[] ids) {
        try {
            Assert.notEmpty(ids, "IDS不能为空");
            productService.delProduct(ids);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("商品删除失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }

    /**
     * 商品导出
     * 1、到出选中的，参数就接收ID
     * 2、导出筛选后的，参数就接收查询对象（结果过多Excel装不下，可以加个最大数限制）
     *
     * @param query
     */
    @PostMapping("/export")
    @ApiOperation(value = "商品导出", response = ApiResponse.class)
    public void exportData(@RequestBody ProductQuery query) {
        productService.exportData(query);
    }

    /**
     * 商品导入
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/import")
    @ApiOperation(value = "商品导入", response = ApiResponse.class)
    public ApiResponse importData(@RequestParam("file") MultipartFile[] file) throws IOException {
        return ApiResponse.fireSuccess(productService.importData(file));
    }

    /**
     * 根据文件名下载模板文件
     *
     * @param fileName 文件名
     * @param response
     * @return
     */
    @GetMapping("/download")
    @ApiOperation(value = "根据文件名下载模板文件", response = ApiResponse.class)
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        productService.download(fileName, response);
    }

    /**
     * 商品上下架
     *
     * @param ids    上下架的ID集合
     * @param status 上下架状态
     * @return
     */
    @GetMapping("/isUp")
    @ApiOperation(value = "商品上下架", response = ApiResponse.class)
    public ApiResponse isUp(@RequestParam String[] ids, @RequestParam Integer status) {
        try {
            Assert.notEmpty(ids, "IDS不能为空");
            Assert.notNull(status, "状态不能为空");
            productService.isUp(ids, status);
            return ApiResponse.fireSuccess();
        } catch (IllegalArgumentException e) {
            log.error("商品删除失败：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
    }


}
